package com.example.minefieldkeepercloud.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.example.minefieldkeepercloud.LoginAc;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.activity.staffmanager.AddStaffActivity;
import com.example.minefieldkeepercloud.adapter.MineFilldAdapter;
import com.example.minefieldkeepercloud.adapter.StaffAdapter;
import com.example.minefieldkeepercloud.bean.MineFieldBean;
import com.example.minefieldkeepercloud.bean.StaffBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 员工管理
 */
public class StaffManagerFragment extends Fragment {

    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;

    @BindView(R.id.tv_sign)
    TextView tv_sign;

    @BindView(R.id.ll_del)
    LinearLayout ll_del;//下房删除布局
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<StaffBean.DataBean> dataBeanList;
    private StaffAdapter staffAdapter;//员工列表

    private boolean isShowCheck;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_smf, container, false);
        ButterKnife.bind(this, view);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();

        getData();
    }


    @OnClick({R.id.tv_sign, R.id.ll_addmine})
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.tv_sign:
                //标记

                if (isShowCheck) {
                    //复选框消失
                    isShowCheck = false;
                    staffAdapter.setCheckBox(false);
                    staffAdapter.notifyDataSetChanged();
                    tv_sign.setText("标记");
                    ll_del.setVisibility(View.GONE);
                    ll_del.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popbottomhidden));
                } else {
                    isShowCheck = true;
                    staffAdapter.setCheckBox(true);
                    staffAdapter.notifyDataSetChanged();
                    tv_sign.setText("完成");
                    ll_del.setVisibility(View.VISIBLE);
                    ll_del.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popbottomshow));
                }
                break;
            case R.id.ll_addmine:
                //添加矿场
                startActivityForResult(new Intent(getActivity(), AddStaffActivity.class),10);
                break;

        }
    }

    /**
     * 清空刷新数据
     */
    private void refreshClear() {
        dataBeanList.clear();
        staffAdapter.notifyDataSetChanged();

        getData();
    }

    /**
     * 加载网络数据
     */
    private void getData() {



        if (NetWorkUtil.check(getActivity()) ) {


            NetHelperNew.GetFactoryUserInFo( new ApiHttpCallback(getActivity(), 1, true) {
                @Override
                public void onSuccess(String data) {

                    StaffBean staffBean = MyGson.getInstance().fromJson(data, StaffBean.class);


                    if (staffBean.getType() == 1) {



                        mRecyclerView.refreshComplete(10);

                        //更新数据
                        dataBeanList.addAll(staffBean.getData());

                        staffAdapter.refrashMinerNotifyDataSetChanged();




                    } else {
                        mRecyclerView.refreshComplete(10);
                        ToastUtil.showToast(getActivity(), staffBean.getContent());
//                        new ErrorDialog(getActivity(), staffBean.getContent()).show();
                    }


                }

            });
        }

    }


    /**
     * 初始化
     */
    private void initview() {


        dataBeanList = new ArrayList<>();
        staffAdapter = new StaffAdapter(getActivity(), dataBeanList, new StaffAdapter.OnCheckBoxListener() {
            @Override
            public void clickCheck() {
                //全选按钮取消
//                checkbox.setChecked(false);


//                Map<Integer, Boolean> isCheck = mineFilldAdapter.getIsCheck();


            }
        });

        lRecyclerViewAdapter = new LRecyclerViewAdapter(staffAdapter);
        //空白页
        mRecyclerView.setEmptyView(getView().findViewById(R.id.empty));
        getView().findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshClear();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);

        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.d_10)
                .setPadding(R.dimen.d_0)
                .setColorResource(R.color.transparent)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {

                refreshClear();
            }
        });
        //停止下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadMoreEnabled(false);
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
//                getData(pagenum, false);
            }
        });

    }
}
